<?php
class Zendvn_Model_Table_LinkContent extends Zend_Db_Table{
	protected $_name = "link_content";
	protected $_primary = "id"; 
	protected $_arrParam = array();

	
	protected $_listSQL;

	public function getItem($arrParam = null, $options = null){
		if($options['task'] == 'get-content-item'){
			$db = $this->getAdapter();

        	$select = $db->select()
			            ->from($this->_name)
						->where('fk_news_item = ?', $arrParam['id_item'])
			            ->where('status = 1');
			$result = $db->fetchRow($select);
        	return $result; 
		}
	}

	public function saveItem($arrParam = null){
		$db = $this->getAdapter();

        $checkID = $this->_getLinkContentItem($arrParam['id_item']);

        $data = array(
            'title'         => $arrParam['title'],
            'description'   => $arrParam['description'],
            'domain'        => $arrParam['domain'],
            'images'        => json_encode($arrParam['images']),
            'status'        => 1

        );

        if(!empty($checkID)){
            $data['updated'] = date('Y-m-d H:i:s');
            $db->update($this->_name,$data,'id = '.$checkID);
        }else{
            $data['created'] = date('Y-m-d H:i:s');
            $data['updated'] = date('Y-m-d H:i:s');
            $data['fk_news_item'] = $arrParam['id_item'];

            $db->insert($this->_name,$data);
        }

	}

    public function updateItemByID($arrParam,$id){
        $db = $this->getAdapter();
        $data = array(
            'title'         => $arrParam['title'],
            'description'   => $arrParam['description'],
            'domain'        => $arrParam['domain'],
            'images'        => json_encode($arrParam['images']),
            'status'        => 2,
            'updated'       => date('Y-m-d H:i:s'),

        );

        $db->update($this->_name,$data,'id = '.$id);
    }
    public function listItem($arrParam = null){
        $db = $this->getAdapter();

        $select = $db->select()
            ->from('link_content AS q',array('id','domain'))
            ->where('status = 1');

        return $db->fetchAll($select);
    }

    public function deleteItem($fk_news_item){
        $db = $this->getAdapter();
        $db->delete($this->_name,'fk_news_item = '.$fk_news_item);
    }
    private function _getLinkContentItem($id){
        $db = $this->getAdapter();

        $select = $db->select()
                     ->from('link_content AS q','id')
                     ->where('fk_news_item = ?',$id)
        ;

        return $db->fetchOne($select);
    }
	

}